package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.CarPackageAuditHistory;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditHistoryMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.CarPackageAuditHistoryQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditHistoryService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CarPackageAuditHistoryServiceImpl implements ICarPackageAuditHistoryService {

    @Autowired
    private CarPackageAuditHistoryMapper carPackageAuditHistoryMapper;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private ServiceItemMapper serviceItemMapper;

    @Override
    public TablePageInfo<CarPackageAuditHistory> query(CarPackageAuditHistoryQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<CarPackageAuditHistory>(carPackageAuditHistoryMapper.selectForList(qo));
    }


    @Override
    public void save(CarPackageAuditHistory carPackageAuditHistory) {
        carPackageAuditHistoryMapper.insert(carPackageAuditHistory);
    }

    @Override
    public CarPackageAuditHistory get(Long id) {
        return carPackageAuditHistoryMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(CarPackageAuditHistory carPackageAuditHistory) {
        carPackageAuditHistoryMapper.updateByPrimaryKey(carPackageAuditHistory);
    }

    @Override
    public void deleteBatch(Long id) {
        carPackageAuditHistoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<CarPackageAuditHistory> list() {
        return carPackageAuditHistoryMapper.selectAll();
    }

    @Override
    public InputStream getProcessImageByAuditId(Long id) {
        CarPackageAuditHistory audit = carPackageAuditHistoryMapper.selectByPrimaryKey(id);
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(audit.getBpmnInfoId());
        InputStream inputStream = null;
        //流程文件使用代码方式画出来
        BpmnModel model = repositoryService.getBpmnModel(bpmnInfo.getActProcessId());
        ProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
        //1:审核中
        if (CarPackageAudit.STATUS_IN_ROGRESS.equals(audit.getStatus())) {

            Task task = taskService.createTaskQuery()
                    .processInstanceId(audit.getInstanceId().toString()).singleResult();
            List<String> activeActivityIds =
                    runtimeService.getActiveActivityIds(task.getExecutionId());

            inputStream = generator.generateDiagram(model, activeActivityIds,
                    Collections.EMPTY_LIST,
                    "宋体", "宋体", "宋体");
        } else {
            //2：审核结束
            inputStream = generator.generateDiagram(model, Collections.EMPTY_LIST, Collections.EMPTY_LIST,
                    "宋体", "宋体", "宋体");
        }
        return inputStream;
    }

}
